package com.xncoding.pos.service.thread.original;

import cn.hutool.core.date.DateUtil;
import com.xncoding.pos.dao.TbUserDao;
import com.xncoding.pos.model.TbUser;
import com.xncoding.pos.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * 线程执行
 *
 * @Author guoyb
 * @Date 2020/7/15  16:51
 */
@Slf4j
public class MyThreadFromCallable implements Callable<String> {

    private TbUserDao tbUserDao;

    private Long id;

    public MyThreadFromCallable(TbUserDao tbUserDao, Long id) {
        this.tbUserDao = tbUserDao;
        this.id = id;
    }

    @Override
    public String call() throws Exception {
        log.info("MyThreadFromCallable业务处理-----打印日志------{}", id);
        if (id>1) {
            TimeUnit.SECONDS.sleep(id);
        }
        TbUser tbUser = tbUserDao.queryById(id);
        TbUser user = Optional.ofNullable(tbUser).orElse(new TbUser());
        log.info("Callable.queryById()-----打印日志------{}", user.toString());
        return JsonUtils.toJson(user);
    }
}
